<template>
  <div class="chart-item small">
    <div class="chart-header">
      <h3>门店销售排名</h3>
      <div class="date-selector">
        <el-select v-model="selectedTimeRange" size="small" @change="handleTimeRangeChange">
          <el-option label="今日" value="today"></el-option>
          <el-option label="本周" value="week"></el-option>
          <el-option label="本月" value="month"></el-option>
          <el-option label="本年" value="year"></el-option>
        </el-select>
      </div>
    </div>
    <div class="store-ranking">
      <div
        v-for="(store, index) in storeRanking"
        :key="store.id"
        class="ranking-item"
      >
        <div class="rank-number" :class="`rank-${index + 1}`">{{ index + 1 }}</div>
        <div class="store-info">
          <div class="store-name">{{ store.name }}</div>
          <div class="store-count">{{ formatNumber(store.count) }}</div>
        </div>
        <div class="growth-rate" :class="store.growth > 0 ? 'positive' : 'negative'">
          {{ store.growth }}%
          <i :class="store.growth > 0 ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"></i>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'StoreRanking',
  props: {
    storeRanking: {
      type: Array,
      default: () => [
        { id: 1, name: '工程店 0 号店', count: 323234, growth: 26 },
        { id: 2, name: '工程店 1 号店', count: 323234, growth: 39 },
        { id: 3, name: '工程店 2 号店', count: 323234, growth: 45 },
        { id: 4, name: '工程店 3 号店', count: 323234, growth: 1 },
        { id: 5, name: '工程店 4 号店', count: 323234, growth: -5 },
        { id: 6, name: '工程店 5 号店', count: 323234, growth: 78 },
        { id: 7, name: '工程店 6 号店', count: 323234, growth: -10 }
      ]
    }
  },
  data() {
    return {
      selectedTimeRange: 'month'
    }
  },
  methods: {
    formatNumber(num) {
      if (num >= 10000) {
        return (num / 10000).toFixed(1) + '万'
      }
      return num.toLocaleString()
    },
    handleTimeRangeChange(range) {
      this.$emit('time-range-change', range)
    }
  }
}
</script>

<style lang="scss" scoped>
.chart-item {
  background: white;
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  
  &.small {
    flex: 1;
    min-width: 320px;
  }
}

.chart-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
  
  h3 {
    font-size: 16px;
    font-weight: 600;
    color: #303133;
    margin: 0;
  }
  
  .date-selector {
    ::v-deep .el-select {
      width: 80px;
    }
  }
}

.store-ranking {
  max-height: 280px;
  overflow-y: auto;
  
  .ranking-item {
    display: flex;
    align-items: center;
    padding: 12px 8px;
    border-bottom: 1px solid #f0f0f0;
    
    &:last-child {
      border-bottom: none;
    }
    
    .rank-number {
      width: 28px;
      height: 28px;
      border-radius: 50%;
      display: flex;
      align-items: center;
      justify-content: center;
      font-weight: bold;
      font-size: 14px;
      margin-right: 12px;
      
      &.rank-1 {
        background: #ffd700;
        color: white;
      }
      
      &.rank-2 {
        background: #c0c0c0;
        color: white;
      }
      
      &.rank-3 {
        background: #cd7f32;
        color: white;
      }
      
      &:not(.rank-1):not(.rank-2):not(.rank-3) {
        background: #f5f5f5;
        color: #666;
      }
    }
    
    .store-info {
      flex: 1;
      
      .store-name {
        font-size: 14px;
        color: #303133;
        margin-bottom: 4px;
      }
      
      .store-count {
        font-size: 12px;
        color: #666;
      }
    }
    
    .growth-rate {
      font-size: 12px;
      font-weight: 500;
      
      &.positive {
        color: #67c23a;
      }
      
      &.negative {
        color: #f56c6c;
      }
    }
  }
}
</style>
